library(tidyverse)

DATA        <- tribble(
  ~FACET                                                                   ,	~THERMO                                            ,	~PE     , ~CILO    , ~CIHI    ,
  "Controls for demographics"                                              , "Rated immigrants cold and colder than each of WBLA", 0.1815833,	0.1469363, 0.2162302,
  "Controls for demographics"                                              , "Rated immigrants equal to each of WBLA"            , 0.4547293, 0.3970855, 0.5123731,
  "Controls for demographics"                                              , "Rated immigrants warm and warmer than each of WBLA", 0.7873915,	0.7526487, 0.8221342,
  "Controls for demographics and partisanship"                             , "Rated immigrants cold and colder than each of WBLA", 0.1109174,	0.0626564, 0.1591784,
  "Controls for demographics and partisanship"                             , "Rated immigrants equal to each of WBLA"            , 0.3660748,	0.2812286, 0.450921 ,
  "Controls for demographics and partisanship"                             , "Rated immigrants warm and warmer than each of WBLA", 0.7040548, 0.6398385, 0.768271 ,
  "Controls for demographics, partisanship, and ratings about other groups", "Rated immigrants cold and colder than each of WBLA", 0.3573528, 0.2583106, 0.456395 ,
  "Controls for demographics, partisanship, and ratings about other groups", "Rated immigrants equal to each of WBLA"            , 0.6172373, 0.5029376, 0.7315371,
  "Controls for demographics, partisanship, and ratings about other groups", "Rated immigrants warm and warmer than each of WBLA", 0.8083799, 0.7449645, 0.8717952)

DATA        <- mutate(DATA, THERMO = case_when(
  THERMO == "Rated immigrants cold and colder than each of WBLA" ~ "Rated immigrants cold and\ncolder than each of WBLA",
  THERMO == "Rated immigrants equal to each of WBLA"             ~ "Rated immigrants equal\nto each of WBLA"             ,
  THERMO == "Rated immigrants warm and warmer than each of WBLA" ~ "Rated immigrants warm and\nwarmer than each of WBLA"
                                            ))

DATA$FACET  <- factor(DATA$FACET , levels = unique(DATA$FACET))
DATA$THERMO <- factor(DATA$THERMO, levels = rev(unique(DATA$THERMO)))

COLOR.COLD <- "indianred2"
COLOR.WARM <- "darkseagreen3"

theme.z <-   theme(
  axis.text.x        = element_blank(),
  axis.text.x.top    = element_text(size = 15, color = "black", hjust = 1  , margin = margin(t = 8,b = 0)),
  axis.text.y        = element_text(size = 15, color = "black", hjust = 1  , margin = margin(l = 8,r = 8)),
  axis.text.y.right  = element_text(size = 15, color = "black", hjust = 0  , margin = margin(l = 8,r = 8)),
  axis.ticks.x       = element_blank(),
  axis.ticks.y       = element_blank(),
  axis.title.x       = element_text(size = 15, color = "black", hjust = 0.5, margin = margin(t = 8,b = 0)),
  axis.title.y       = element_blank(),
  legend.position    = "none",
  panel.background   = element_rect(size = 0.5, color = "black", fill = "gray90", linetype = "solid"),
  panel.border       = element_rect(size = 1.0, color = "black", fill = NA      , linetype = "solid"),
  panel.grid.major.x = element_blank(), 
  panel.grid.major.y = element_blank(),
  panel.grid.minor.x = element_blank(), 
  panel.grid.minor.y = element_blank(),
  panel.spacing.x    = unit(4, "lines"),
  panel.spacing.y    = unit(1, "lines"),
  plot.background    = element_rect(fill = "white"),
  plot.caption       = element_text(size = 12, hjust = 0  , margin = margin(t = 10)),
  plot.margin        = unit(c(t = 10, r = 45, b = 10, l = 0),"pt"),
  plot.subtitle      = element_text(size = 15, hjust = 0.5, margin = margin(b = 10)),
  plot.title         = element_text(size = 17, hjust = 0.5, margin = margin(t = 0, b = 10), face = "bold"),
  strip.background   = element_rect(size = 1.0, color="black", fill="black", inherit.blank = FALSE),
  strip.text.x       = element_text(color="white", face="bold", size=15, margin = margin(t = 12, b = 12))
)

ggplot(DATA, aes(x = PE, y = THERMO)) +
  facet_wrap(~FACET, ncol = 1,  dir = "v", labeller = label_wrap_gen(width = 28)) +
  geom_rect(data = filter(DATA, FACET == "Controls for demographics"                                                  ), aes(xmin = min(DATA$PE[DATA$FACET == "Controls for demographics"]                                                  ), xmax = DATA$PE[DATA$FACET == "Controls for demographics"                                                   & DATA$THERMO == "Rated immigrants equal\nto each of WBLA"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.COLD, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Controls for demographics"                                                  ), aes(xmax = max(DATA$PE[DATA$FACET == "Controls for demographics"]                                                  ), xmin = DATA$PE[DATA$FACET == "Controls for demographics"                                                   & DATA$THERMO == "Rated immigrants equal\nto each of WBLA"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.WARM, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Controls for demographics and partisanship"), aes(xmin = min(DATA$PE[DATA$FACET == "Controls for demographics and partisanship"]), xmax = DATA$PE[DATA$FACET == "Controls for demographics and partisanship" & DATA$THERMO == "Rated immigrants equal\nto each of WBLA"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.COLD, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Controls for demographics and partisanship"), aes(xmax = max(DATA$PE[DATA$FACET == "Controls for demographics and partisanship"]), xmin = DATA$PE[DATA$FACET == "Controls for demographics and partisanship" & DATA$THERMO == "Rated immigrants equal\nto each of WBLA"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.WARM, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Controls for demographics, partisanship, and ratings about other groups"), aes(xmin = min(DATA$PE[DATA$FACET == "Controls for demographics, partisanship, and ratings about other groups"]), xmax = DATA$PE[DATA$FACET == "Controls for demographics, partisanship, and ratings about other groups" & DATA$THERMO == "Rated immigrants equal\nto each of WBLA"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.COLD, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Controls for demographics, partisanship, and ratings about other groups"), aes(xmax = max(DATA$PE[DATA$FACET == "Controls for demographics, partisanship, and ratings about other groups"]), xmin = DATA$PE[DATA$FACET == "Controls for demographics, partisanship, and ratings about other groups" & DATA$THERMO == "Rated immigrants equal\nto each of WBLA"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.WARM, inherit.aes = FALSE) +
  geom_errorbarh(aes(xmin = CILO, xmax = CIHI), height = 0, size = 0.5) +
  geom_point(size = 4) +
  geom_text(x = 1.05, y = DATA$THERMO, size = 5.5, hjust = 0, label = format(round(DATA$PE, 2), nsmall = 2)) +
  coord_cartesian(clip = "off") +
  scale_x_continuous(limits = c(0,1), expand = c(0,0)) +
  labs(x = "Predicted probability of a vote\nfor Joe Biden over Donald Trump") +
  theme.z

ggsave(file = "Figure 4.svg", width = 6.5, height = 9.5)
ggsave(file = "Figure 4.eps", width = 6.5, height = 9.5)
